package c2.cross.server;

import c2.advanced.C2Advanced;
import c2.cross.business.CrossBody;
import cate.game.GameBody;
import cp.server.CpServerTemplate;
import cp.server.template.adv.CrossSrvLauncher;
import cp.solution.CpSolutionCommon;
import cp.solution.business.dog.cmd.StartCrossCmd;
import easy.java.EasyJava;
import easy.java.spring.scaffold.EnableEasySpringScaffold;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import reactor.core.publisher.Hooks;

@Slf4j
@EnableEasySpringScaffold
@SpringBootApplication
public class CrossServerApplication {

    public static StartCrossCmd cmd = new StartCrossCmd();

    public static CrossSrvLauncher launcher = new CrossSrvLauncher();

    public static void main(String[] args) {
        cmd.parseFromArgs(args);
        Hooks.onErrorDropped(e -> {});
        new SpringApplicationBuilder(CrossServerApplication.class)
                .run(args);
        log.info("版本信息：{} {} {} {} {} {}",
                CrossBody.version(),
                GameBody.version(),
                C2Advanced.version(),
                CpSolutionCommon.version(),
                CpServerTemplate.version(),
                EasyJava.version());
    }

    /**
     * 强制退出
     */
    public static void shutdownForce(String message) {
        log.info("执行强制退出！ 因为 {}", message);
        Runtime.getRuntime().halt(0);
    }

    /**
     * 退出
     */
    public static void shutdown(String message) {
        log.info("尝试执行shutdown, 因为 {}", message);
        if (C2Advanced.ctx == null) {
            log.error("GrossServerApplication.ctx==null shutdown拒绝执行");
            return;
        }
        int exitCode = SpringApplication.exit(C2Advanced.ctx);
        System.exit(exitCode);
    }
}
